package org.feiye.studyday24;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

//读文件
public class ReadFileTest {
	public static void main(String[] args) {
		InputStream f = null;
		try {
			// 创建输入流对象
			f = new FileInputStream("F:\\Java课件\\1.Java语言基础\\java_homework\\第一章\\java-basic-1-作业.txt");
			System.out.println("可以读到的字节数：" + f.available());
			int available = f.available();
			// read()方法读文件

			// int r;
//			while ((r = f.read()) != -1) {
//				System.out.print((char) r);
//			}

			// read(byte[] b)方法读文件

			byte[] b = new byte[available];
			int r = f.read(b);
			for (byte c : b) {
				System.out.print((char) c);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			try {
				f.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
